package Util;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.widget.ImageView;

import com.bumptech.glide.DrawableTypeRequest;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.example.lib_base.R;


/**
 * Created by admin on 2018/1/22.
 */

public class ImageLoader {

    public static void load(Context context, ImageView iv, String url) {
        load(context, iv, url, null);
    }
    public static void load(Context context, ImageView iv, String url, String refererUrl) {
        Drawable error;

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            error = context.getDrawable(R.drawable.error);
        } else {
            error = context.getResources().getDrawable(R.drawable.error);
        }
//一般直接通过网址加载
        Glide.with(context).load(url).error(error).into(iv);
//一下一般省略
//        getRequest(context, url, refererUrl)
//                .skipMemoryCache(true)
//                .error(error)
//                .into(iv);
    }

    /** 生成请求 */
    private static DrawableTypeRequest getRequest(Context context, String url, String refererUrl) {

        //网络
        LazyHeaders.Builder builder = new LazyHeaders.Builder();
        if (refererUrl != null) {
            builder.addHeader("Accept", "image/webp,*/*;q=0.8");
            builder.addHeader("Accept-Encoding","gzip, deflate, sdch");
            builder.addHeader("Accept-Language","zh-CN,zh;q=0.8");
            builder.addHeader("Cache-Control", "no-cache");
            builder.addHeader("Connection", "keep-alive");
            builder.addHeader("Referer", refererUrl);
            builder.addHeader("Host","i1.100av.xyz");
            builder.addHeader("Pragma", "no-cache");
            builder.addHeader("Cookie", "__cfduid=d1092314ff7c1b7748ffe14babb0e69791491966502");
            builder.addHeader("User-Agent","ozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.154 Safari/537.36 LBBROWSER");
        }

        return Glide.with(context).load(new GlideUrl(url, builder.build()));
    }


}



